package com.lzj.myapplication.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by lizj on 2018/3/30.
 */

public class MyView extends View {
    private int XPoint = 60;   //x轴起始的位置
    private int YPoint = 860;  //Y轴起始的位置
    private int XScale = 40;   //X轴刻度长度
    private int YScale = 40;   //Y轴刻度长度
    private int XLength = 8000; //X轴总长度
    private int YLength = 800; //Y轴总长度

    private int XRate = 1; //X轴倍率
    private int YRate = 10; //Y轴倍率

    private int MaxDataSize = XLength / XScale;

    private List<Integer> data = new ArrayList<Integer>();

    private List<String> YLabel = new ArrayList<String>();
    private List<String> XLabel = new ArrayList<String>();



//    private String[] YLabel = new String[YLength / YScale];
//    private String[] XLabel = new String[XLength / XScale];

    private Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            if(msg.what == 0x1234){
                MyView.this.invalidate();
            }
        };
    };
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);

//        for(int i=0; i<YLabel.length; i++){
//            YLabel[i] = YRate*i + "";
//        }
//
//        for(int i=0; i<XLabel.length; i++){
//            XLabel[i] = (i + XRate) + "s";
//        }

        for(int i=0; i<YLength / YScale; i++){
            YLabel.add(i,YRate*i + "");
        }

        for(int i=0; i<XLength / XScale; i++){
            XLabel.add(i,(i + XRate) + "s");
        }

        new Thread(new Runnable() {

            @Override
            public void run() {
                while(true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(data.size() >= MaxDataSize){
                        data.remove(0);
                    }
                    if(data.size() >= 20){
                        data.remove(0);
                        
                    }


                    data.add(new Random().nextInt(100) + 1);
                    handler.sendEmptyMessage(0x1234);
                }
            }
        }).start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true); //去锯齿
        paint.setColor(Color.BLUE);
        paint.setTextSize(20);
        //画Y轴
        canvas.drawLine(XPoint, YPoint - YLength, XPoint, YPoint, paint);

        //Y轴箭头
        canvas.drawLine(XPoint, YPoint - YLength, XPoint - 3, YPoint-YLength + 6, paint);  //箭头
        canvas.drawLine(XPoint, YPoint - YLength, XPoint + 3, YPoint-YLength + 6 ,paint);

        //添加Y轴刻度和文字
        for(int i=0; i * YScale < YLength; i++) {
            canvas.drawLine(XPoint, YPoint - i * YScale, XPoint + 5, YPoint - i * YScale, paint);  //刻度

            canvas.drawText(YLabel.get(i), XPoint - 50, YPoint - i * YScale, paint);//文字
        }



        //画X轴
        canvas.drawLine(XPoint, YPoint, XPoint + XLength, YPoint, paint);

        //X轴箭头
//        canvas.drawLine(XLength+XPoint, YLength, XLength+XPoint - 6, YLength - 3, paint);  //箭头
//        canvas.drawLine(XLength+XPoint, YLength, XLength+XPoint - 6, YLength + 3 ,paint);

        //添加X轴刻度和文字
        for(int i=0; i * XScale < XLength; i++) {
            canvas.drawLine(XPoint+i*XScale, YPoint , XPoint+i*XScale, YPoint+5, paint);  //刻度

            canvas.drawText(XLabel.get(i), XPoint+i*XScale, YPoint+50 , paint);//文字
        }

        System.out.println("Data.size = " + data.size());
        if(data.size() > 1){
            for(int i=1; i<data.size(); i++){
                Log.i("lzj","大小为： "+data.get(i-1));
                myInterFace.getValue(data.get(i)+"");
                canvas.drawLine(XPoint + (i-XRate) * XScale, YPoint - data.get(i-1) * YScale/YRate,
                        XPoint + i * XScale, YPoint - data.get(i) * YScale/YRate, paint);
            }
        }
    }

    private MyInterface myInterFace;//1.声明接口

    public void setMyInterFace(MyInterface myInterFace) { //2.给接口设置set方法
        this.myInterFace = myInterFace;
    }

    public interface MyInterface {

        public void getValue(String value);//获得回调值的接口方法


    }
}
